#include "ncparseclass.h"

NcParseClass::NcParseClass()
{

}

int NcParseClass::parseToNulanguage(QString &incomeProgram, QStringList &cmdStrListOut,
                                    QVector<PointPro> &pointListOut,
                                     QStringList &messageOut)
{

    qDebug()<<"NC incomeProgram"<<incomeProgram;

    Program outputProgram;
    int currentPointIndex=0;
    QStringList textLineList = incomeProgram.split("\n"); // 需要写入的模板数据
    QVector<double> currentPose;
    currentPose.resize(6);
    // 遍历所有行
    QRegExp rx("\\S*\\s*G(\\d*).*");
    QRegExp rx_pValue;
    Line tmpLine;
    PointPro tmpPoint;
    for(int i=0;i<textLineList.size();i++)
    {
        qDebug()<<"thisLine:"<<textLineList[i];
        bool isMatch=rx.exactMatch(textLineList[i]);
        if(false==isMatch )
        {
            qDebug()<<"unknow非Ｇ 指令"<<textLineList[i];
            continue;
        }

        switch (rx.cap(1).toInt())
        {
        case 0://G0
        {
            rx_pValue.setPattern("\\S*\\s*G\\d*\\s.*X(\\+?-?\\d*.?\\d*).*");
            isMatch=rx_pValue.exactMatch(textLineList[i]);
            if(isMatch )
            {
                currentPose[0]=rx_pValue.cap(1).toDouble();
            }

            rx_pValue.setPattern("\\S*\\s*G\\d*\\s.*Y(\\+?-?\\d*.?\\d*).*");
            isMatch=rx_pValue.exactMatch(textLineList[i]);
            if(isMatch )
            {
                currentPose[1]=rx_pValue.cap(1).toDouble();
            }

            rx_pValue.setPattern("\\S*\\s*G\\d*\\s.*Z(\\+?-?\\d*.?\\d*).*");
            isMatch=rx_pValue.exactMatch(textLineList[i]);
            if(isMatch )
            {
                currentPose[2]=rx_pValue.cap(1).toDouble();
            }

            tmpLine.type=MOTION;
            tmpLine.motion.motionFormat=E_MOTION_J;
            ++currentPointIndex;
            tmpLine.motion.p=currentPointIndex;
            tmpLine.motion.cnt=0;
            tmpLine.motion.rate=100;
            outputProgram.append(tmpLine);

            tmpPoint.jointFlag=1;
            tmpPoint.ufValue=defaultUserCoordNumber;
            tmpPoint.utValue=defaultToolCoordNumber;
            tmpPoint.positionValue=currentPose.toStdVector();
            tmpPoint.pointNum=tmpLine.motion.p;
            pointListOut.append(tmpPoint);
        }

            break;
        case 2:
        case 3:
        case 1://G1
        {
            rx_pValue.setPattern("\\S*\\s*G\\d*\\s.*X(\\+?-?\\d*.?\\d*).*");
            isMatch=rx_pValue.exactMatch(textLineList[i]);
            if(isMatch )
            {
                currentPose[0]=rx_pValue.cap(1).toDouble();
            }

            rx_pValue.setPattern("\\S*\\s*G\\d*\\s.*Y(\\+?-?\\d*.?\\d*).*");
            isMatch=rx_pValue.exactMatch(textLineList[i]);
            if(isMatch )
            {
                currentPose[1]=rx_pValue.cap(1).toDouble();
            }

            rx_pValue.setPattern("\\S*\\s*G\\d*\\s.*Z(\\+?-?\\d*.?\\d*).*");
            isMatch=rx_pValue.exactMatch(textLineList[i]);
            if(isMatch )
            {
                currentPose[2]=rx_pValue.cap(1).toDouble();
            }
            tmpLine.type=MOTION;
            tmpLine.motion.motionFormat=E_MOTION_L;
            ++currentPointIndex;
            tmpLine.motion.p=currentPointIndex;
            tmpLine.motion.cnt=100;
            tmpLine.motion.rate=100;
            outputProgram.append(tmpLine);
            tmpPoint.jointFlag=1;
            tmpPoint.ufValue=defaultUserCoordNumber;
            tmpPoint.utValue=defaultToolCoordNumber;
            tmpPoint.positionValue=currentPose.toStdVector();
            tmpPoint.pointNum=tmpLine.motion.p;
            pointListOut.append(tmpPoint);
        }
//        case 2:
//        {

//        }
//        case 3:
//        {

//        }
        break;
        default:
        {
            qDebug()<<"不支持　Ｇ 指令"<<textLineList[i];
            continue;
        }
        break;
        }
    }

    transProgramStructToString(outputProgram,cmdStrListOut );
    return 1;

}
